var page = require('webpage').create();
var args = require('system').args;
phantom.page.injectJs(args[1]); //include the config file

page.open(args[2], function() {
    page.includeJs(config.jquerypath, function() {

        var evaluatePage = page.evaluate(function(config) {
            var properties = {};
            var pass = 'pass';
            var fail = 'fail'; //constants
            var i = 0;

            //Scan the Page for the font
            properties.font = {};
            properties.font.status = pass;
            properties.font.presentOnPage = config.fonts;
            var currentFont = '';
            $('div,h1,h2,h3,h4,h5,p,span,td,tr,a').each(function() {
                currentFont = $(this).css('font-family').toLowerCase();
                if (currentFont.indexOf(config.fonts[0]) < 0 && currentFont.indexOf(config.fonts[1]) < 0) {
                    properties.font.status = fail;
                    properties.font.presentOnPage = currentFont;
                    return false;
                }
            });

            //Scan the footer and social media elements
            properties.footerLinks = {};
            properties.socialMediaLinks = {};

            for (i = 0; i < config.footerLinks.length; i++) {
                properties.footerLinks[config.footerLinks[i]] = {};
                properties.footerLinks[config.footerLinks[i]].linkFound = fail;
                properties.footerLinks[config.footerLinks[i]].linkDirection = fail;
            }

            for (i = 0; i < config.socialMediaLinks.length; i++) {
                properties.socialMediaLinks[config.socialMediaLinks[i]] = {};
                properties.socialMediaLinks[config.socialMediaLinks[i]].linkFound = fail;
                properties.socialMediaLinks[config.socialMediaLinks[i]].link = fail;
            }

            $('a').each(function() {
                currentLinkText = $(this).text().toLowerCase();
                currentLinkHref = $(this).attr('href') + "";
                currentLinkHref = currentLinkHref.toLowerCase();
                for (i = 0; i < config.footerLinks.length; i++) {
                    if (config.footerLinks[i] == currentLinkText) {
                        properties.footerLinks[config.footerLinks[i]].linkFound = pass;
                        if ($(this).css('float') == config.footerLinkDirection) {
                            properties.footerLinks[config.footerLinks[i]].linkDirection = pass;
                        }
                    }
                }

                for (i = 0; i < config.socialMediaLinks.length; i++) {
                    if (currentLinkHref.indexOf(config.socialMediaLinks[i]) > -1) {
                        properties.socialMediaLinks[config.socialMediaLinks[i]].linkFound = pass;
                        properties.socialMediaLinks[config.socialMediaLinks[i]].link = currentLinkHref;
                    }
                }

            });

            //Check for google analytics
            properties.googleAnalytics = {};
            properties.googleAnalytics.found = fail;
            properties.googleAnalytics.src   = fail;
            $('script').each(function() {
            	currentSrc = $(this).attr('src') + "";
                for (i = 0; i < config.googleAnalytics.length; i++) {
                    if (currentSrc.indexOf(config.googleAnalytics[i]) > -1) {
                        properties.googleAnalytics.found = pass;
            			properties.googleAnalytics.src   = currentSrc;
                    }
                }
            });
            
            return properties;
        }, config);

        console.log(JSON.stringify(evaluatePage));
        phantom.exit();
    });
});